import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      children: [
        {
          path: '',
          name: 'Welcome',
          component: () => import('../views/Welcome.vue'),
        },
        {
          path: 'BOMList',
          name: 'BOMList',
          component: () => import('../views/BOMList.vue'),
        },
        {
          path: 'BOMCreate',
          name: 'BOMCreate',
          component: () => import('../views/BOMCreate.vue'),
        },
        {
          path: 'BOMEdit',
          name: 'BOMEdit',
          component: () => import('../views/BOMCreate.vue'),
        },
        {
          path: 'BOMDetails',
          name: 'BOMDetails',
          component: () => import('../views/BOMDetails.vue'),
        },
        
        {
          path: 'WeatherWarning',
          name: 'WeatherWarning',
          component: () => import('../views/WeatherWarning.vue'),
        },
      ],

    },
   
  ],
})

// 简易登录守卫（不使用 JWT）：无 token 跳登录；有 token 访问登录则跳转列表
router.beforeEach((to, _from, next) => {
  const token = localStorage.getItem('erp_token')
  if (!token && to.name !== 'Login') {
    next({ name: 'Login' })
    return
  }
  if (token && to.name === 'Login') {
    next({ name: 'Welcome' })
    return
  }
  next()
})

export default router
